package com.lvyou.service;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.lvyou.entity.Goods;
import com.lvyou.entity.Orders;
import com.lvyou.mapper.IGoodsMapper;
import com.lvyou.mapper.IOrdersMapper;
import com.lvyou.util.TurnPage;
import com.lvyou.util.UUIDTools;

@Service("OrderSrv")
public class OrderSrv {

	@Resource
	private IOrdersMapper dao;
	
	@Resource
	private IGoodsMapper goodsDao;
	
	public List<Orders> queryList(int status,String userId,TurnPage turnPage) throws Exception{
	
		return dao.queryList(status,userId,turnPage);
	}
	
	public Orders queryById(String id) throws Exception{
		return dao.queryById(id);
	}
	
	public int add(Orders order) throws Exception{

		Goods goods=goodsDao.queryById(order.getGoodsId());

		order.setId(UUIDTools.randomUUID());
		order.setGoodsName(goods.getName());
		order.setGoodsDescrip(goods.getDescrip());
		order.setPrice(goods.getPrice());
		order.setTotalPrice(order.getPersonNum()*goods.getPrice());
		order.setStatus(1);
		Date date=new Date();
		order.setCreateTime(date);
		order.setUpdateTime(date);
		
		goodsDao.updatePersonNumById(order.getPersonNum(), order.getGoodsId());
		
		return dao.add(order);
	}
	
	public int deleteByIds(String[] ids) throws Exception{
		return dao.deleteByIds(ids);
	}
}
